#ifndef TELNETSOCKET_H
#define TELNETSOCKET_H

#include <QTcpSocket>
#include <QMap>
class QTimer;
class WaitForAck;

class TelnetSocket : public QTcpSocket
{
    Q_OBJECT
public:
    explicit TelnetSocket(quint16 port,QByteArray nodeIP,qint64 subAddr,QObject *parent = 0);

signals:
    //连接断开。
    void sigTelnetDisconnectedTemporarily(QByteArray nodeIP,qint64 subAddr);
    void sigTelnetConnected(QByteArray nodeIP, qint64 subAddr);
    void sigExitThread(QByteArray nodeIP,qint64 subAddr);
    void sigShowInfoToTextEdit(QByteArray text);
    void sigPacketReady(QByteArray packet);

    void sigShowInfoToErrorTextEdit(QByteArray text);
private slots:
    void slotTcpConnected();
    void slotTcpDisconnected();
    void slotProcessTcpDatagrams();
    void slotTcpError();
    void slotTimerSingleShotForReconnect();
    void slotPacketToNodes(QByteArray packet,qint64 destSubAddr);
    void slotTimerEventSendTcpData();
    void slotTimerEventSendHeartPacket();

public slots:

private:
    void doWithConfiACK(QByteArray data,qint64 nodeAddr,quint8 sN);
    WaitForAck *waitForACK;

    quint16 telnetPort;
    QByteArray telnetIP;
    //当某个节点自己是sink时，它会发送不同节点的数据包。该变量存储这些数据包的源地址。（低7字节）
    QList <qint64> listNodesSubAddr;

    QList <qint64> listNodesAddrForHeart; //存储节点的地址，以便向中间件发送心跳

    QByteArray tcpData;

    qint16 timerIntervalSeconds; //定时器间隔
    qreal timerIntervalFactor;   //每次定时器间隔乘以这么多
    qint16 reconnectTimes; //统计断开后重连次数

    QTimer *timerSendTcpData;
    QList <QByteArray> listTcpPacket;

    QTimer *timerSendHeartPacket; //控制发送心跳包的定时器

    qint64 countTTL;
};

#endif // TELNETSOCKET_H
